{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "627eaa18-4947-4771-ba25-9d2134c638f0",
   "metadata": {},
   "source": [
    "# Parameter `sizeUnit` in Geoms `geomPoint()`, `geomText()` and `geomLabel()`\n",
    "\n",
    "The `sizeUnit` parameter relates the size of a geometry to the length of the unit step along one of the axes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "90fae1e1-22be-4032-9aaf-ef8bf6c26e95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"k5IxWV\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.3.1/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"k5IxWV\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"k5IxWV\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "950a2cb4-3679-4b6a-a64b-180b380f53cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.7.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.1."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "47564c6d-2c90-4187-b658-4ddb7e2f4790",
   "metadata": {},
   "outputs": [],
   "source": [
    "val size = 10\n",
    "val data = mapOf(\n",
    "    \"x\" to listOf(0, 0, 0),\n",
    "    \"y\" to listOf(-size, 0, size),\n",
    "    \"text\" to listOf('A', 'b', '0')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9433c9c0-60ed-4e4d-bbd9-c0424044b4e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun title(geomName: String, sizeUnit: String? = null) = ggtitle(\"$geomName: size = $size, sizeUnit = ${sizeUnit ?: \"default\"}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "beef10e9-91f2-4478-acd9-dfa2fe5be278",
   "metadata": {},
   "outputs": [],
   "source": [
    "val p = letsPlot(data) { x = \"x\"; y = \"y\" } + scaleYContinuous(limits = -size to size, expand = listOf(0.15, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6768c245-29fa-4a93-820f-1259be17d55d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"4PrMSQ\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Point: size = 10, sizeUnit = default\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,0.0,0.0],\n",
       "\"y\":[-10.0,0.0,10.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"expand\":[0.15,0],\n",
       "\"limits\":[-10.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":10.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Point: size = 10, sizeUnit = y\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,0.0,0.0],\n",
       "\"y\":[-10.0,0.0,10.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"expand\":[0.15,0],\n",
       "\"limits\":[-10.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":10.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"size_unit\":\"y\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"4PrMSQ\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(\n",
    "    p + geomPoint(size = size) + title(\"Point\"),\n",
    "    p + geomPoint(size = size, sizeUnit =\"y\") + title(\"Point\", \"y\")\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "98c03a57-2c1d-4cea-bc4b-a41940942e0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"GKx9Cx\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Text: size = 10, sizeUnit = default\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,0.0,0.0],\n",
       "\"y\":[-10.0,0.0,10.0],\n",
       "\"text\":[\"A\",\"b\",\"0\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"expand\":[0.15,0],\n",
       "\"limits\":[-10.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"label\":\"text\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":10.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Text: size = 10, sizeUnit = y\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,0.0,0.0],\n",
       "\"y\":[-10.0,0.0,10.0],\n",
       "\"text\":[\"A\",\"b\",\"0\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"expand\":[0.15,0],\n",
       "\"limits\":[-10.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"label\":\"text\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":10.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"text\",\n",
       "\"size_unit\":\"y\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"GKx9Cx\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(\n",
    "    p + geomText(size = size) { label = \"text\" } + title(\"Text\"),\n",
    "    p + geomText(size = size, sizeUnit = \"y\") { label = \"text\" } + title(\"Text\", \"y\"),\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dbe6f50d-7615-478f-bac8-0637be9b015d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"w7AneA\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Label: size = 10, sizeUnit = default\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,0.0,0.0],\n",
       "\"y\":[-10.0,0.0,10.0],\n",
       "\"text\":[\"A\",\"b\",\"0\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"expand\":[0.15,0],\n",
       "\"limits\":[-10.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"label\":\"text\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":10.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"label\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Label: size = 10, sizeUnit = y\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[0.0,0.0,0.0],\n",
       "\"y\":[-10.0,0.0,10.0],\n",
       "\"text\":[\"A\",\"b\",\"0\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"expand\":[0.15,0],\n",
       "\"limits\":[-10.0,10.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"label\":\"text\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":10.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"label\",\n",
       "\"size_unit\":\"y\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"w7AneA\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(\n",
    "    p + geomLabel(size = size) { label = \"text\" } + title(\"Label\"),\n",
    "    p + geomLabel(size = size, sizeUnit = \"y\") { label = \"text\" } + title(\"Label\", \"y\"),\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ae3078e-478c-435d-b978-c7f5de658a52",
   "metadata": {},
   "source": [
    "#### Use Case: Drawing a Venn Diagram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "16073823-3e3c-49a2-89a7-b91c94ed2957",
   "metadata": {},
   "outputs": [],
   "source": [
    "val r = 0.5\n",
    "val h = r * sqrt(3.0) / 2\n",
    "val setsData = mapOf(\n",
    "    \"x\" to listOf(0, r, r / 2),\n",
    "    \"y\" to listOf(0, 0, h),\n",
    "    \"size\" to listOf(2 * r, 2 * r, 2 * r),\n",
    "    \"color\" to listOf(\"#e24462\", \"#306998\", \"#ffe873\"),\n",
    ")\n",
    "val textsData = mapOf(\n",
    "    \"x\" to listOf(-r / 2, r + r / 2, r / 2, r / 2, -r / 8, r + r / 8, r / 2),\n",
    "    \"y\" to listOf(-r / 4, -r / 4, h + r / 2, -3 * r / 8, 5 * r / 8, 5 * r / 8, 5 * r / 16),\n",
    "    \"text\" to listOf(\"Kotlin\", \"Geospatial\\nVisualization\", \"Grammar\\nof\\nGraphics\", \"ArcGIS\", \"Kandy\", \"ggmap\", \"Lets-Plot\"),\n",
    "    \"size\" to List(7) { 0.4 * r }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dfacab46-9c64-4fab-8f79-65375e7946c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"myo07d\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"fixed\",\n",
       "\"flip\":false\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":600.0,\n",
       "\"height\":600.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"size\",\n",
       "\"scale_mapper_kind\":\"identity\",\n",
       "\"guide\":\"none\"\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"scale_mapper_kind\":\"identity\",\n",
       "\"guide\":\"none\"\n",
       "},{\n",
       "\"aesthetic\":\"x\",\n",
       "\"limits\":[-0.5,1.0]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"limits\":[-0.5,0.9330127018922193]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"color\",\n",
       "\"size\":\"size\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"size\":[1.0,1.0,1.0],\n",
       "\"color\":[\"#e24462\",\"#306998\",\"#ffe873\"],\n",
       "\"x\":[0.0,0.5,0.25],\n",
       "\"y\":[0.0,0.0,0.4330127018922193]\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"shape\":21.0,\n",
       "\"alpha\":0.5,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"tooltips\":\"none\",\n",
       "\"size_unit\":\"x\"\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"label\":\"text\",\n",
       "\"size\":\"size\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"size\":[0.2,0.2,0.2,0.2,0.2,0.2,0.2],\n",
       "\"x\":[-0.25,0.75,0.25,0.25,-0.0625,0.5625,0.25],\n",
       "\"y\":[-0.125,-0.125,0.6830127018922193,-0.1875,0.3125,0.3125,0.15625],\n",
       "\"text\":[\"Kotlin\",\"Geospatial\\nVisualization\",\"Grammar\\nof\\nGraphics\",\"ArcGIS\",\"Kandy\",\"ggmap\",\"Lets-Plot\"]\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"text\",\n",
       "\"size_unit\":\"x\"\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"classic\",\n",
       "\"axis\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"line\":{\n",
       "\"blank\":true\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"myo07d\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot() +\n",
    "    geomPoint(data = setsData, sizeUnit = \"x\",\n",
    "              shape = 21, alpha = .5, color = \"black\",\n",
    "              tooltips = tooltipsNone) { \n",
    "        x = \"x\"; y = \"y\"; size = \"size\"; fill = \"color\"\n",
    "    } +\n",
    "    geomText(data = textsData, sizeUnit = \"x\") {\n",
    "        x = \"x\"; y = \"y\"; label = \"text\"; size = \"size\"\n",
    "    } +\n",
    "    scaleSizeIdentity() + scaleFillIdentity() +\n",
    "    xlim(-r to 2 * r) + ylim(-r to h + r) +\n",
    "    coordFixed() +\n",
    "    ggsize(600, 600) +\n",
    "    themeVoid()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
